package daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import dao.MessageDao;

import pojo.Message;
import pojo.MessageComment;
import pojo.User;
import utils.DateUtils;
import utils.DruidUtils;


public class MessageDaoImpl implements MessageDao{

	@Override
	public ArrayList<Message> getAllMessage() {
		Connection conn = DruidUtils.getConnection();
		String sql = "select * from message";
		ArrayList<Message> meList = new ArrayList<Message>();
		try {
			PreparedStatement pre = conn.prepareStatement(sql);
			
			ResultSet res = pre.executeQuery();
			while(res.next()) {
				Message me = new Message();
				me.setMid(res.getInt("mid"));
				me.setmTitle(res.getString("mTitle"));
				me.setmContent(res.getString("mContent"));
				me.setmLoveNum(res.getInt("mLoveNum"));
				me.setmForkNum(res.getInt("mForkNum"));
				me.setmCommentNum(res.getInt("mCommentNum"));
				me.setmReportNum(res.getInt("mReportNum"));
				me.setUserid(res.getInt("userid"));
				meList.add(me);
			}
			res.close();
			pre.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return meList;
	}

	@Override
	public Message getSingleMessage(int mid) {
		Connection conn = DruidUtils.getConnection();
		String sql = "select * from message where mid="+mid;
		Message me = new Message();
		try {
			PreparedStatement pre = conn.prepareStatement(sql);
			
			ResultSet res = pre.executeQuery();
			while(res.next()) {
				me.setMid(res.getInt("mid"));
				me.setmTitle(res.getString("mTitle"));
				me.setmContent(res.getString("mContent"));
				me.setmLoveNum(res.getInt("mLoveNum"));
				me.setmForkNum(res.getInt("mForkNum"));
				me.setmCommentNum(res.getInt("mCommentNum"));
				me.setmReportNum(res.getInt("mReportNum"));
				me.setUserid(res.getInt("userid"));

			}
			res.close();
			pre.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return me;
	}

	@Override
	public User getUserInfo(int userid) {
		Connection conn = DruidUtils.getConnection();
		String sql = "select * from user where userid="+userid;
		User user = new User();
		try {
			PreparedStatement pre = conn.prepareStatement(sql);
			
			ResultSet res = pre.executeQuery();
			while(res.next()) {
				user.setUserid(res.getInt("userid"));
				user.setUserName(res.getString("userName"));
				user.setUserIma(res.getString("userIma"));
				user.setUserSex(res.getInt("userSex"));
				user.setMoney(res.getInt("money"));
				user.setMotto(res.getString("motto"));
			}
			res.close();
			pre.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public ArrayList<MessageComment> getMessageComment(int mid) {
		Connection conn = DruidUtils.getConnection();
		String sql = "select * from (select messagecomment.*,user.userIma,user.userName from messagecomment,user where messagecomment.userid=user.userid) as temp where mid="+mid;
		ArrayList<MessageComment> mcList = new ArrayList<MessageComment>();
		try {
			PreparedStatement pre = conn.prepareStatement(sql);
			
			ResultSet res = pre.executeQuery();
			while(res.next()) {
				MessageComment mc = new MessageComment();
				mc.setMcid(res.getInt("mcid"));
				mc.setMid(res.getInt("mid"));
				mc.setUserid(res.getInt("userid"));
				mc.setUserIma(res.getString("userIma"));
				mc.setUserName(res.getString("userName"));
				mc.setMcContent(res.getString("mcContent"));
				mc.setMcTime(res.getDate("mcTime"));
				mc.setMcLoveNum(res.getInt("mcLoveNum"));
				mcList.add(mc);
			}
			res.close();
			pre.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mcList;
	}

	@Override
	public int sendMcComment(int mid, int userid, String mcComment) {
		Connection conn = DruidUtils.getConnection();
		String sql = "insert into messagecomment values(0,?,?,?,?,0)";
		PreparedStatement pre = null;
		try {
			pre = conn.prepareStatement(sql);
			pre.setInt(1, mid);
			pre.setInt(2, userid);
			pre.setString(3, mcComment);
			pre.setString(4, DateUtils.formDate(new Date(), "yyyy-MM-dd hh:mm:ss"));
			return pre.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				pre.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}
	

}
